<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $imgs = DB::table('image')->get();
        return view('home.image_index')->with('imgs',$imgs);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // return view('home.image_add');
         return view('home.ajax_image_add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //单文件上传
        /*$img = $request->file("img");
        if($request->hasFile("img")){
            //确认上传的文件是否成功
            if($img->isValid()){
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $name = time().rand(1000,9999).".".$ext;
                $img->move("./upload/",$name);  
                $url =  "./upload/".$name;
                DB::table('image')->insert(['url'=>$url]);
                return redirect('image');
               
                // if($res > 0){
                //     echo "添加成功";
                //     return redirect('user');
                // }else{
                //     echo "添加失败";
                // }
            }
        }*/

        //多文件上传 注意路径
        $imgs = $request->file('img');
        $urls=[];
        foreach($imgs as $img){
            if($img->isValid()){
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                $path = './upload/';
                $name = "origin_".time().rand(10,100).'.'.$ext;
                if($img->move($path,$name)){
                    $url = '/upload/'.$name;
                    $urls[]=['url'=>$url];
                }
            }
        }
        DB::table('image')->insert($urls);
        // return redirect('image');
        //ajax返回
        $data =[
            'status'=>200,
            'msg'=>'成功',
            'data'=>$urls
        ];
        // return $data;
        echo json_encode($data);
   

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)   
    {
        return view('home.image_add');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
